
/**
 * 单个医嘱单保存-》发送-》打印
 * @param that
 * @param isSend
 * @param dangeFlag
 * @returns {Boolean}
 */
var savePrintList_dange = {};//发送医嘱校验是否需要回退的医嘱数组
var tabFlag=[];//单个医嘱单打印的激活页签标识
function saveAdvice_dange(that,isSend)
{
	 thisbutton = that;
	$(thisbutton).attr("disabled",true);
	execIframe();
	if (isSend == "send")
	{
		layer.msg('发送中....',
		{
		icon : 16,
		skin : 'layui-layer-msg16',
		shade : [
		0.1, '#393D49'
		],
		time : 0
		});
	}
	else
	{
		layer.msg('保存中....',
		{
		icon : 16,
		skin : 'layui-layer-msg16',
		shade : [
		0.1, '#393D49'
		],
		time : 0
		});
	}
var illageflag = false;
$("#new_order_list").find("li").find(".messageSign").each(function(ind,ele){
	if($(ele).is(":visible")&&$(ele)[0].style.backgroundColor == "red"){
		illageflag = true;
		return false;
	}
})

if(illageflag){
	layer.msg('请检查手工输入的医嘱!',{
	    time : 2000,
		icon : 0,
		skin : 'layui-layer-msg0'});
	$(thisbutton).removeAttr("disabled");
	return false;
}
	validFlg = true;
	validRestult = '';
	xiyaochangdu = [];
	zhongyaochangdu = 0;
	// 编辑前删除
	var vaf01s = "";
	var cbm01 = $("#cbm01").val();
	if (cbm01)
	{
		var editArr = $(window.parent.document).find(".yizhu_details");
		for (var i = 0; i < editArr.length; i++)
		{
			var editStr = $(editArr[i]).attr("data-info");
			var editObj = JSON.parse(editStr);
			if (editObj.cbm01 == cbm01)
			{
				vaf01s += editObj.vaf01 + ",";
			}
		}
		var url = rootUrl + "/clideladvice/removeAdvice";
		var data =
		{
		"vaf01s" : vaf01s,
		"bce03" : bce03
		};

		$.ajax(
		{
		url : url,
		type : "post",
		data : data,
		async : false,
		dataType : 'json',
		success : function(data)
		{
		}
		});

	}
debugger;
	var curtab = $("#order_tab").find(".active");
	var cur_top = $("#new_order_list").find("li.active a").attr("href");
	var tableId;
	var tableclass;//中药是class
	if(cur_top.indexOf("#Zhongyao") >=0){
		tableclass = $(curtab).find(".zhongyao_table");
	}else{
		tableId = $(curtab).find("table").attr("id");
		
	}
	// 西药
	var tableObj = $("#"+tableId);
	var bby01Arr_1 = [];
	var bby01Arr_11 = [];// 储存西、成药品需要数量和库存数量的json数组
	$(tableObj).find("tbody tr").each(function()
	{
		var bby01 = $(this).find(".xiyao_yznr").next().next().val();
		if (bby01 != "" && bby01 != undefined)
		{
			var ypmc = $(this).find(".xiyao_yznr").val().replace(/[\r\n]/g,"");
			var vaf21 = $(this).find(".xiyao_zl").val(); // 数量
			var temp = {};
			temp.bby01 = bby01;
			temp.vaf21 = vaf21;
			temp.ypmc = ypmc;
			temp.zxks = $(this).find(".xiyao_zxks").attr("id");
			bby01Arr_11.push(temp);
			bby01Arr_1.push(temp);
		}
	});

	// 中药
	debugger;
	var tableObj = $(tableclass).find("tbody tr");
//	var bby01Arr_3 = [];
//	var bby01Arr_31 = [];// 储存中草药品需要数量和库存数量的json数组
	$(tableObj).each(function()
	{
		var bby01 = $(this).find(".zhongyao_yznr").next().next().val();
		if (bby01 != "" && bby01 != undefined)
		{
			var vaf21 = $(this).find(".zhongyao_dl").val(); // 中药数量
			var ypmc = $(this).find(".zhongyao_yznr").val().replace(/[\r\n]/g,"");
			var temp = {};
			temp.bby01 = bby01;
			temp.vaf21 = vaf21;
			temp.ypmc = ypmc;
			temp.zxks = $(this).parents(".china_medicine").find(".china_medicine_com").find(".zhongyao_zxks select").val();
			bby01Arr_11.push(temp);
			bby01Arr_1.push(temp);
		}
	});

	// 确认医嘱前检验药品库存是否足够
	var url = rootUrl + "/doctoradviceeditor/checkdrugstoreNew";
	var params = {bck01_x:valueArr['p_100_105002_108'],bck01_z:valueArr['p_100_105002_110']};
	params.lACF01 = 1;
	params.bby01Arr_1 = JSON.stringify(bby01Arr_1);
//	params.bby01Arr_3 = JSON.stringify(bby01Arr_3);

	var msg = "";// 库存不够时的提示信息
	$.post(url, params, function(data)
	{
		debugger;
		var list = data.list;
		for (var i = 0; i < list.length; i++)
		{
			for (var j = 0; j < bby01Arr_11.length; j++)
			{
				if (bby01Arr_11[j]["bby01"] == list[i]["bby01"])
				{
					bby01Arr_11[j].amount = list[i]["amount"];
					bby01Arr_11[j].bby03 = list[i]["bby03"];
				}
			}
		}
			for (var i = 0; i < bby01Arr_11.length; i++)
			{
				if (!bby01Arr_11[i]["amount"])
				{
					msg += bby01Arr_1[i]["ypmc"] + "--需要数量为" + bby01Arr_11[i]["vaf21"] + ",当前库存数量为0;<br/>";
				}
				else if ((Number(bby01Arr_11[i]["amount"]) < Number(bby01Arr_11[i]["vaf21"])))
				{
					console.log(bby01Arr_11[i]);
					msg += bby01Arr_11[i]["ypmc"] + "--需要数量为" + bby01Arr_11[i]["vaf21"] + ",当前库存数量为" + bby01Arr_11[i]["amount"] + ";<br/>";
				}
			}
		if ("" != msg)
		{
			layer.alert(msg,
			{
				title : '库存不足提示'
			});
			$(thisbutton).removeAttr("disabled");
		}
		else
		{
			// 数据合法性校验
			saveAdviceBegin_dange(isSend);
		}
	});
}
//var sendFlag = true;//控制发送流程
/**
 * @param isSend
 * @returns {Boolean}
 */
function saveAdviceBegin_dange(isSend)
{
	var bck01a = bck01;
	// 组装json
	var cbm1 = [];
	var vao1 = [];
	var vaf1 = [];
	var vbq1 = [];
	var vax1 = [];
	var vbf1 = [];
	var advice_details = [];//智慧医保audit
	var vaoLength = $(".bootstrap-tagsinput").children("span").length;
	/*
	 * 手动诊断前先保存为疾病信息
	 */
	var jibingArr = [];
	for (var i = 0; i < vaoLength; i++)
	{
		var id = $($(".bootstrap-tagsinput").children("span")[i]).attr("data-info");
		if (id == -999)
		{
			var cam1 = {};
			var temp = $($(".bootstrap-tagsinput").children("span")[i]).text();
			cam1.cam02 = 1;
			cam1.cam04 = temp;// 名称
			cam1.cam06 = bce03;// 编者
			cam1.abbrp = makePy(temp)[0];// 中文转换为拼音首字母
			jibingArr.push(cam1);
		}
	}
	/*if (jibingArr.length > 0)
	{
		var url = rootUrl + "/clinewadvice/cliniccaminsert";
		var params = {};
		params.cam1 = JSON.stringify(jibingArr);
		$.ajax(
		{
		url : url,
		data : params,
		dataType : 'json',
		async : false,
		type : 'post',
		success : function(data)
		{
			var list = data.list;
			for (var i = 0; i < list.length; i++)
			{
				cam1Map[list[i]['cam04']] = list[i]['cam01'];
			}
		}
		});
	}*/
	var curtab = $("#order_tab").find(".active");
	var cur_top = $("#new_order_list").find("li.active a").attr("href");
	var tableId;
	var tableclass;//中药是class
	if(cur_top.indexOf("#Zhongyao") >=0){
		tableclass = $(curtab).find(".zhongyao_table");
	}else{
		tableId = $(curtab).find("table").attr("id");
		
	}
	switch (topTab)
	{
		case "Xiyao" :
			GetInfoFromTable1(tableId,vaf1, cbm1, vax1, vao1,advice_details);// 西药
			break;
		case "Zhongyao" :
			GetInfoFromTable3(tableclass,vaf1, cbm1, vax1, vao1,advice_details);// 中药
			break;
		case "Jianyan" :
			GetInfoFromTableL(tableId,vaf1, cbm1, vax1, vao1,advice_details);// 检验
			break;
		case "Jiancha" :
			GetInfoFromTableE(tableId,vaf1, cbm1, vax1, vao1, vbq1,advice_details);// 检查
			break;
		case "Shoushu" :
			GetInfoFromTableS(tableId,vaf1, cbm1, vax1, vao1,vbf1,advice_details);// 手术
			break;
		case "Zhiliao" :
			GetInfoFromTableT(tableId,vaf1, cbm1, vax1, vao1,advice_details);// 治疗
			break;
		case "Cailiao" :
			GetInfoFromTable4(tableId,vaf1, cbm1, vax1, vao1,advice_details);// 材料
			break;
	}
	if(topTab=="Xiyao" || topTab=="Zhongyao")
	{
		//药品处方没有诊断的校验
		if(vaf1.length>0 && vao1.length==0)
		{
			layer.msg('药品处方无诊断不能保存！',{time:3000,icon:0,skin: 'layui-layer-msg0'});  
			$(thisbutton).removeAttr("disabled");
			return true;
		}
		
		var totalLength = valueArr.p_100_105002_146;
		xiyaochangdu = xiyaochangdu.unique2();//去重
		if ((xiyaochangdu.length) > totalLength)
		{
			var index = layer.alert('限制药品条数最多为'+totalLength+'条，若要更改，可在参数设置中更改设置！',
			{
			skin : '#fff', // 样式类名
			closeBtn : 0
			}, function()
			{
				layer.close(index);
			});
			$(thisbutton).removeAttr("disabled");
			return;
		}
		if (valueArr.p_100_105002_354 == "1")
		{
			if (xiyaochangdu.length&& zhongyaochangdu)
			{
				var index = layer.alert('医嘱单不允许对应多个药房，若要对应，可在参数设置中更改设置！',
				{
				skin : '#fff', // 样式类名
				closeBtn : 0
				}, function()
				{
					layer.close(index);
				});
				return;
				$(thisbutton).removeAttr("disabled");
			}
		}
	}
	var lxml = {};
	lxml.vao1 = vao1;
	lxml.vaf1 = vaf1;
	lxml.cbm1 = cbm1;
	lxml.vax1 = vax1;
	lxml.vbq1 = vbq1;
	lxml.vbf1 = vbf1;
	
	var data1 = {};
	data1.lxml = JSON.stringify(lxml);
	data1.ltype = 1;
	data1.lmode = 0;
	data1.lacf01 = 1;
	data1.lbck01 = bck01a;
	data1.lbce01 = bce01;
	data1.lbce02 = 1;
	data1.lbce03 = bce03;
	data1.rcbm = '';
	data1.lCBM01A = 0;
	console.table(vaf1);
	audit_dange(advice_details,vaf1,data1,isSend);
	
}

function afterAudit_dange(vaf1, data1, isSend)
{
	if (!validFlg)
	{
		if(validRestult){
			layer.msg(validRestult,
			{
			time : 2000,
			icon : 2,
			skin : 'layui-layer-msg2'
			});
		}

		$(thisbutton).removeAttr("disabled");
	}
	else
	{
		if (vaf1.length == 0)
		{
			layer.msg('没有需要保存的医嘱，请检查！',
			{
			time : 2000,
			icon : 0,
			skin : 'layui-layer-msg0'
			});
			$(thisbutton).removeAttr("disabled");
		}
		else
		{
			var obj = {};
		    obj.isSend = isSend;
		    obj.data1 = data1;
			sendAdviceByzhenjin_dange(obj);
		}
	}

}

/**
 * 直接发送医嘱
 * 
 * @param list
 */
function sendImmediately_dange(obj)
{
	var list = obj.list,zjAddList = obj.zjAddList;
	var senArr = [];
	var vbd1 = [];
	var vaf01s = "(";
	var vaf1 = "[";
	var cbm01Arr = [];
	for (var i = 0; i < list.length; i++)
	{
		if (list[i].bda01 == "T" && list[i].vaf32 == "0")
		{
			senArr.push(list[i])
		}
		else if (list[i].bda01 != "T" && list[i].bda01 != "3")
		{
			senArr.push(list[i])
		}
	}
	if (senArr)
	{
		var cbm01Arr_send = [];// 生成计价信息用
		for (var i = 0; i < senArr.length; i++)
		{
			var bda01 = senArr[i].bda01;
			var vaf01a = senArr[i].vaf01a;
			var vaf32 = senArr[i].vaf32;
			if (judgeFlag(bda01, vaf01a, vaf32))
			{
				var vaf01 = senArr[i].vaf01;
				vaf1 += "{";
				vaf1 += "\"vaf01\":" + vaf01;
				var vaf10 = senArr[i].vaf10;
				var bda01 = senArr[i].bda01;
				var bby01 = senArr[i].bby01;
				var bby25 = senArr[i].bby25;
				var dsk01 = senArr[i].dsk01;
				var bck01b = senArr[i].bck01b;
				var cbm01 = senArr[i].cbm01;
				cbm01Arr_send.push(cbm01);
				if ("LE".indexOf(bda01) >= 0)
				{
					var obj = {};
					obj.cbm01 = cbm01;
					obj.bda01 = bda01;
					cbm01Arr.push(obj);
				}
				if (i > 0)
				{
					vaf01s += "," + vaf01;
				}
				else
				{
					vaf01s += vaf01;
				}
				vaf1 += "}";
				// -----------------------------------------

				if (i < senArr.length - 1)
				{
					vaf1 += ",";
				}
			}
		}
		cbm01Arr_send = cbm01Arr_send.unique2();// 去重
		for (var i = 0; i < cbm01Arr_send.length; i++)
		{
			vbdInfoForsend_final(cbm01Arr_send[i]);
			for (var j = 0; j < vbdList.length; j++)
			{
				var vbdT = {};
				vbdT.vaf01 = vbdList[j]["vaf01"];
				vbdT.bby01 = vbdList[j]["bby01"];
				vbdT.vbd04 = vbdList[j]["vaf21"];// 数量
				vbdT.bby25 = vbdList[j]['bby25'];// 剂量单价
				totalVbi += (vbdT.vbd04 * 1) * (vbdT.bby25 * 1);// 遍历累加消费金额
				vbdT.vbd05 = vbdList[j]['lprice'];// 门诊包装单价
				vbdT.vbd06 = vbdList[j]['bdu06'];// 是否有从项
				vbdT.bck01 = vbdList[j]['bck01'];// 执行科室
				vbdT.bdn01 = vbdList[j]['bdn01'];// 医嘱项目类型
				vbdT.vbd08 = vbdList[j]['bdu07'];// 费用性质
				vbdT.vbd09 = vbdList[j]['bdu10'];// 收费方式
				vbdT.dsk01 = vbdList[j]['dsk01'];// 药品批次
				vbdT.bby01a = vbdList[j]['bby01a'];// 收费套餐ID
				vbdT.vbd12 = vbdList[j]['vbd12'];// 诊疗收费对照数量
				vbdT.bag23 = vbdList[j]['bag23'];// 
				vbdT.bag05 = vbdList[j]['bag05'];// 
				vbdT.bby05 = vbdList[j]['bby05'];// 
				vbd1.push(vbdT);
			}
		}

		vaf01s += ")";
		vaf1 += "]";
//		var xml = "{'VAF1':" + vaf1 + ",'VBD1':" + JSON.stringify(vbd1) + "}";
		var xmlObj = {};
		xmlObj.VAF1 = vaf1;
		xmlObj.VBD1 = vbd1;
		xmlObj.zjAddList = zjAddList;
		var xml = JSON.stringify(xmlObj);
		var vaa01 = $("#vaa01").val();
		var vac01 = $("#vac01").val();
		var send_savePrintList = [];
		var curtab = $("#order_tab").find(".active").attr("id");
		debugger;
		if($.inArray(curtab, tabFlag) >-1 )
		{
			send_savePrintList = savePrintList_dange[curtab];
		}
		// 开始异步发送
		$.ajax(
		{
		async : true,
		url : rootUrl + "/clisendadvice/clinicadvicesend",
		data :
		{
		"savePrintList":JSON.stringify(send_savePrintList),
		"lXml" : xml,
		"lSign" : 2,
		"lBCE01" : bce01,
		"lBCE02" : "",
		"lBCE03" : bce03,
		"lBCK01" : bck01,
		"lACF01" : 1,
		"lVAA07" : vac01,
		"lVAA01" : vaa01,
		},
		dataType : "json",
		method : "post",
		success : function(data)
		{
			if (data.result == "Success")
			{
				debugger;
				var curtab = $("#order_tab").find(".active").attr("id");
				tabFlag.push(curtab);
				
				//父页面只存储需要打印的值
//				parent.savePrintList_dange = data.list;
//				parent.savePrintList = $.merge(parent.savePrintList,data.list);
				
				//新开页面存储需要回退的值
				savePrintList_dange[curtab] = data.list;
				savePrintList = $.merge(savePrintList,data.list);
				layer.msg('发送成功',
				{
				time : 3000,
				icon : 1,
				skin : 'layui-layer-msg1'
				});
				// 开始更新医嘱抄送记录和生成诊疗申请单
				var url9 = rootUrl + "/clidoctor/nurse_Lis_Bills_Print_batch";
				var params9 = {};
				params9.bce03 = bce03;
				params9.cbm01Arr = JSON.stringify(cbm01Arr);
				var temp_vbq = {};
					temp_vbq.vbq27 = $(".e_jianchamudi").val();
					temp_vbq.vbq28 = $(".e_linchuangzhenduan").val();
					temp_vbq.vbq29 = $(".e_binglizhaiyao").val();
					temp_vbq.vbq30 = $(".e_fuzhujiancha").val();
				params9.vbq1 = JSON.stringify(temp_vbq);
				$.ajax(
				{
				async : true,
				url : url9,
				type : 'post',
				dataType : 'json',
				data : params9,
				success : function(data9)
				{
					// 如果是一卡通扣费用户，执行扣费操作
					var vipId = $("#vipId").text();
					if (false)
//						if (valueArr['p_9999_9999_112']=='1' && vipId != "--")
					{
						var msg = "";
						if ($("#vbu14").text() * 1 < totalVbi)
						{
							msg = "卡内基本余额：" + $("#vbu14").text() + "；本次消费总额：" + totalVbi + "。请先充值。。。";
							layer.alert(msg,
							{
							icon : 1,
							title : "温馨提示："
							});
							return true;
						}
						else
						{
							debugger;
							// msg =
							// "卡内基本余额："+$("#vbu14").text()+"；本次消费总额："+totalVbi+"。";
							// layer.alert(msg, {icon:1,title:"温馨提示："});
							vbu09 = newCardOut(2);// 弹出消费窗口
							vajList = data.rows;
						}
					}
					else
					{
						// 勾选直接打印调用
						var flag = $("#send_is_print").parent().attr("class").indexOf("checked")>=0;
						//判断是否关闭新开界面
						debugger;
						var curtab = $("#order_tab").find(".active").attr("id");
						var flag_url = getUrlParam("yiyuanId");
						var lvac01 = $("#vac01").val();
						if(valueArr['p_100_105002_402']=="0"||flag_url)
						{
							if(data.commList&&data.commList.length>0){
								tipCardMsg(data.commList);
							}
							printIm(flag,savePrintList_dange[curtab]);
							layer.close(zhenjin_index);
							parent.reloadPatientInfo(lvac01);
							$(thisbutton).removeAttr("disabled");
							loadEmrInit();
						}
						else
						{
							var sobj = {};
							sobj.vac01 = $("#vac01").val()
							sobj.flag = flag;
							sobj.list = savePrintList_dange[curtab];
							sobj.sql = getDiagnoseSql();
							if(data.commList&&data.commList.length>0){
								sobj.msg = feeTipMsg(data.commList);
								parent.closeAndPrint(sobj);
								
							}else{
								sobj.msg = false; 
								parent.closeAndPrint(sobj);
							}
//							if(data.commList&&data.commList.length>0){
//								parent.closeAndPrint($("#vac01").val(),flag,savePrintList_dange[curtab],feeTipMsg(data.commList));
//							}else{
//								parent.closeAndPrint($("#vac01").val(),flag,savePrintList_dange[curtab],false);
//								
//							}
						}
					}
				}
				});

			}
			else
			{
				layer.msg(data.resultMsg,
				{
				time : 2000,
				icon : 2,
				skin : 'layui-layer-msg2'
				});
				$(thisbutton).removeAttr("disabled");
			}
		}
		});
	}
}

/**
 * 选择诊查费
 */
function zhenjin2_dange(obj)
{
	$.ajax({
		url:rootUrl+"/clidoctor/queryHasZj",
		type:"get",
		dataType:"json",
		data:{vac01:$("#vac01").val(),hours:valueArr['p_9999_9999_95']},
		async:false,
		success:function(data)
		{
			debugger;
			var flag = data.map.flag;
			if(flag)
			{
				sendFlag = false;
				//查询诊查费列表
				var zjList;
				$.ajax({url:rootUrl+"/clidoctor/queryZjList",type:"get",async:false,dataType:"json",success:function(data){
					zjList = data.list;
				}});
				laytplUrl(myurl + "/module/zhenjin.html",{}, function(data, html) {
					    zhenjin_index = layer.open({
						type : 1,
						// scrollbar : false,
						title : "选择诊查费",
						area : [ "500px", "500px"],
						shade : 0.1,
						maxmin : true,
						content : html,
						closeBtn : 1,
						cancel:function(){
							$(thisbutton).removeAttr("disabled");

							layer.msg("医嘱发送已取消！",{time : 3000,
								icon : 0,
								skin : 'layui-layer-msg0'});
						},
						success : function() {
							$("#zhenjin_table").empty();
							$("#zhenjin_table").GridUnload();
							jQuery("#zhenjin_table").jqGrid(
									{
										data : zjList,
										datatype : "local",
										height : 'auto',
										width : 'auto',
										autowidth : true,
										autoheight : true,
										multiselect : true,
										multiboxonly:true,
										colModel : [
										            {
										            	label:'项目名称',
										            	name : 'bby05',
										            	align : 'left',
										            	width:'300'
										            },
										            {
										            	name : 'bdn01',
										            	hidden : true
										            },
										            {
										            	name : 'bby01',
										            	hidden : true
										            },
										            {
										            	label : '数量',
										            	name : 'amount',
										            	align : "right",
										            	formatter : function(value, options, rData){
										       		    	if(value){
										       		    		 val =value;
										       		    	   }else{
										       		    		 val=1;
										       		    	   }
										       		    	 
										       		    	return val;
										       		    	}
										            },
										            {
										            	label : '单价',
										            	name : 'bby25',
										            	align : "right"
										            },
										            {
										            	label : '金额',
										            	name : 'bby25',
										            	align : "right"
										            }
										            ],
										            ondblClickRow : function(rowid, status)
										            {
										            	var row = $("#zhenjin_table").jqGrid("getRowData", rowid);
										            }
									});
							$("#zhenjin_cancle").on("click",function(){
								var that =this;
								$(thisbutton).removeAttr("disabled");
								layer.msg("医嘱发送已取消！",{time : 3000,
									icon : 0,
									skin : 'layui-layer-msg0'});
								//这里添加弹出提示：是否不添加诊金
								layer.close(zhenjin_index);
							});
							//获取诊金数据
							addZhenji2_dange(obj);
						}
					})

				});
			}
			else
			{
//				sendFlag = true;
				var zjAddList = [];
				obj.zjAddList = zjAddList;
				sendAdviceAfterSave_dange(obj);
			}
		}
	});
}

/**
 * 保存诊金费用1
 */
function addZhenji2_dange(obj)
{
	var zjAddList = [];
	$("#add_zhenji").on("click",function(){
		var that =this;
		$(that).attr("disabled",true);
		var ids = $('#zhenjin_table').jqGrid('getGridParam','selarrrow');
		for (var int = 0; int < ids.length; int++)
		{
			zjAddList.push($("#zhenjin_table").jqGrid("getRowData", ids[int]));
		}
		obj.zjAddList = zjAddList;
		sendAdviceAfterSave_dange(obj);
	});
}

/*--------------------------------------------------发送如果弹出诊金框，点击确定按钮继续发送，否则取消发送------------------------------------------------------------*/
function sendAdviceByzhenjin_dange(obj){
	//发送的时候选择诊金
	if (valueArr['p_9999_9999_104']=="1")
	{
		//弹出诊金对话框,根据诊金选中来确定是否发送医嘱
		zhenjin2_dange(obj);
	}else{
		//直接发送
		var zjAddList = [];
		obj.zjAddList = zjAddList;
		sendAdviceAfterSave_dange(obj)
	}
		
	
}
//保存医嘱方法
function justSaveAdvice_dange(obj){
	var def = $.Deferred();
	$.ajax(
			{
			url : rootUrl + "/clinewadvice/clinicvaf1insert",
			type : "post",
			async : false,
			data : obj.data1,
			dataType : 'json',
			success : function(data)
			{
//				var t4 = new Date().getTime();
//			    console.log('保存遗嘱耗时'+(t4-t3)+'毫秒');
				if (data.result == "Success")
				{
					 def.resolve(data);
					 if (obj.isSend != "send"){
						layer.msg("保存成功！",
						{
						time : 2000,
						icon : 1,
						skin : 'layui-layer-msg1'
						});
						// 关闭并刷新
						closeAndRefresh();
					 }
				}
				else
				{
					
					layer.msg(data.resultMsg,
					{
					icon : 0,
					skin : 'layui-layer-msg0',
					time : 3000
					});
					$(thisbutton).removeAttr("disabled");
					 def.reject();
		        	 return;
				}

			}
			});
	 return def.promise();
}
//保存成功后发送
function sendAdviceAfterSave_dange(obj){
	$.when(
			justSaveAdvice_dange(obj) //先保存
     ).done(function (data) {
    	 //执行发送
    	 	var list = data.list;
    	 	obj.list = list;
    	 	sendImmediately_dange(obj);
     });
}






